package com.ml.mall.service.company.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.company.CompanyAddrDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.company.SysCompanyAddr;
import com.ml.mall.mapper.company.SysCompanyAddrMapper;
import com.ml.mall.service.company.SysCompanyAddrService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysCompanyAddrServiceImpl extends ServiceImpl<SysCompanyAddrMapper, SysCompanyAddr> implements SysCompanyAddrService{

    @Autowired
    private SysCompanyAddrMapper sysCompanyAddrMapper;

    /**
     * 判断公司地址信息是否完整
     * @param
     * @return
     */
    @Override
    public boolean isCompletedAddrInfo(SysCompanyAddr companyAddr, Integer companyType) {
        if(null == companyAddr){
            return false;
        }
        if (null == companyAddr.getCity()
                || null == companyAddr.getCountry()
                || StrUtil.isBlank(companyAddr.getName())
                || null == companyAddr.getPostCode()
                || StrUtil.isBlank(companyAddr.getRegionalAddress())
                || null == companyAddr.getStates()
//                || StrUtil.isBlank(companyAddr.getStreetAddress()) //忽略检测
        ) {
            return false;
        }

        //卖家购物地址信息不检测email,phoneNumber和entityName
        if(!CommonConsts.ROLE_TYPE_SUPPLIER.equals(companyType)){
            if(StrUtil.isBlank(companyAddr.getEmail())
                    || null == companyAddr.getPhoneNumber()
                    || StrUtil.isBlank(companyAddr.getEntityName())){
                return false;
            }
        }
        return true;
    }

    @Override
    public void saveCompanyAddr(CompanyAddrDto billAddrDto, CompanyAddrDto shopAddrDto, Long companyId) {
        if(null == companyId){
            return;
        }

        // 添加账单地址
        if(null != billAddrDto){
            SysCompanyAddr companyAddr = new SysCompanyAddr();
            BeanUtils.copyProperties(billAddrDto, companyAddr);
            companyAddr.setType(CommonConsts.COMPANY_ADDR_TYPE_BILL);
            companyAddr.setCompanyId(companyId);
            sysCompanyAddrMapper.insert(companyAddr);
        }
        // 添加购物地址
        if(null != shopAddrDto){
            SysCompanyAddr companyAddr = new SysCompanyAddr();
            BeanUtils.copyProperties(shopAddrDto,companyAddr);
            companyAddr.setType(CommonConsts.COMPANY_ADDR_TYPE_SHOP);
            companyAddr.setCompanyId(companyId);
            sysCompanyAddrMapper.insert(companyAddr);
        }
    }
}
